import { reqShopCart,reqDeleteCart, reqUpdateChecked } from "@/api";

// 利用vuex存储数据
import { SET_USERID } from '@/utils/USER_ID'

//登录、注册模块的小仓库
let state = {
     //vuex仓库存储用户临时身份,vuex存储数据确实非持久化的，SET_USERID执行返回结果,可是本地存储获取的！！
     USER_ID: SET_USERID(),
     shopCartInfo: [],
};
let mutations = {

     GETSHOPCART(state, payload) {
          state.shopCartInfo = payload;
     }
};
let actions = {

     // 获取用户购物车的数据
     async getShopCart({ commit, state, dispatch }) {
          let result = await reqShopCart();
          if (result.code == 200) {
               commit('GETSHOPCART', result.data)
          }
     },

     // 删除某一个商品
     async  deleteCartById({ commit, state, dispatch },skuId){
          let result = await reqDeleteCart(skuId)
          if(result.code == 200){
               return 'ok'
          }else{
               return Promise.reject()
          }

     },

     // 修改商品的勾选状态
     async changeChecked({ commit, state, dispatch },{skuId,isChecked}){
          let result = await reqUpdateChecked(skuId,isChecked)

          if(result.code == 200){
               return 'ok'
          }else{
               return Promise.reject()
          }

     },

     // 获取复选框的勾选状态
      updateAllChecked({ commit, state, dispatch },isChecked){
          let arr = []
          // 获取购物车商品的个数  进行遍历
          state.shopCartInfo[0].cartInfoList.forEach(item=>{
               // 调用修改某一个商品的action
               let ps = dispatch('deleteCartById',{skuId:item.skuId,isChecked})

               arr.push(ps)
          })

          return Promise.all(arr)

     },

     // 删除选中的商品
     deleteAllCart({ commit, state, dispatch }){
          let arr = [];

          state.shopCartInfo[0].cartInfoList.forEach(item=>{
               if(item.isChecked == 1){
                 let ps = dispatch('deleteCartById',item.skuId)

                 arr.push(ps)
               }
          })

          return Promise.all(arr)

     }
};
let getters = {
     //  //计算数组里面第一个元素：对象
     CartInfo(state){
          return state.shopCartInfo[0] || {};
     }
};

//对外暴露
export default {
     state,
     mutations,
     actions,
     getters
}